package com.qiuzhiyi.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.li.utils.TransformUtil;
import com.li.widget.TopNavigationBar;
import com.qiuzhiyi.Const;
import com.qiuzhiyi.R;
import com.qiuzhiyi.adapter.AgentAdapter;
import com.qiuzhiyi.base.QZYBaseActivity;
import com.qiuzhiyi.item.AgentItem;
import com.qiuzhiyi.item.BaseItem;
import com.qiuzhiyi.net.ObservableHelper;
import com.qiuzhiyi.net.RetrofitHelper;
import com.qiuzhiyi.net.SelectorObserver;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

/**
 * Author: Li
 * Date: 2017/11/14 0014
 * Description: 经纪人界面
 */

public class AgentActivity extends QZYBaseActivity {

    @BindView(R.id.top_navigation)
    TopNavigationBar mTopNavigation;
    @BindView(R.id.recycler)
    RecyclerView mRecycler;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout mRefreshLayout;


    private AgentAdapter mAgentAdapter;
    private List<AgentItem.ListBean> mAgentItemList;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_agent;
    }

    @Override
    protected void initSth() {
        super.initSth();
        mTopNavigation.setNavigationBarListener(this);
        setSmartRefreshLayout(mRefreshLayout);
        mAgentAdapter = new AgentAdapter(R.layout.item_agent);
        mRecycler.setLayoutManager(new LinearLayoutManager(mContext));
        ImageView imageView = new ImageView(mContext);
        imageView.setLayoutParams(new RecyclerView.LayoutParams(-1,
                TransformUtil.dp2px(mContext, 140)));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setImageResource(R.drawable.zhaogong_head);

        mAgentAdapter.setHeaderView(imageView);
        mAgentAdapter.bindToRecyclerView(mRecycler);
        mAgentAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                String phone=mAgentItemList.get(position).getEPhone();
                Intent intent=new Intent(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:"+phone));
                startActivity(intent);
            }
        });
        if (getIntent().getExtras() != null) {
            switch (getIntent().getStringExtra("op")) {
                case "chooseAgent":
                    mAgentAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                        @Override
                        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                            AgentItem.ListBean listBean=mAgentItemList.get(position);
                            final String empId= String.valueOf(listBean.getEId());
                            final String empName=listBean.getEName();

                            String msg="是否选择 "+empName+" 做为您的经纪人";
                            QMUIDialog.MessageDialogBuilder builder=new QMUIDialog.MessageDialogBuilder(mContext)
                                    .setTitle("提示")
                                    .setMessage(msg)
                                    .addAction("取消", new QMUIDialogAction.ActionListener() {
                                        @Override
                                        public void onClick(QMUIDialog dialog, int index) {
                                            dialog.dismiss();
                                        }
                                    })
                                    .addAction("确定", new QMUIDialogAction.ActionListener() {
                                        @Override
                                        public void onClick(QMUIDialog dialog, int index) {
                                            dialog.dismiss();
                                            Bundle bundle=new Bundle();
                                            bundle.putString("empId",empId);
                                            bundle.putString("empName",empName);
                                            Intent intent=new Intent();
                                            intent.putExtras(bundle);
                                            setResult(RESULT_OK,intent);
                                            finish();
                                        }
                                    });
                            builder.create().show();
                        }
                    });
                    break;
            }
        }
        getAgentList("1");
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        super.onRefresh(refreshlayout);
        getAgentList("1");
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        super.onLoadmore(refreshlayout);
        getAgentList(String.valueOf(mPage + 1));
    }

    private void getAgentList(String page) {
        Map<String, String> map = new HashMap<>();
        map.put("pageNum", page);
        map.put("numPerPage", Const.NUM_PER_PAGE);
        ObservableHelper.createObservable(RetrofitHelper.createService().getAgentList(map))
                .subscribe(new SelectorObserver<BaseItem<AgentItem>>() {
                    @Override
                    protected void onErrorCodeInfo(String code, String info) {
                        stopRefreshLoad();
                    }

                    @Override
                    protected void onSuccess(BaseItem<AgentItem> item) {
                        stopRefreshLoad();
                        List<AgentItem.ListBean> tmp = item.getObjValue().getList();
                        if (mIsRefresh) {
                            mPage = 1;
                            mAgentItemList = tmp;
                            mAgentAdapter.setNewData(mAgentItemList);
                            if (item.getObjValue().isIsLastPage()) {
                                mRefreshLayout.setLoadmoreFinished(true);
                            } else {
                                mRefreshLayout.setLoadmoreFinished(false);
                            }

                        } else {
                            if (item.getObjValue().isIsLastPage()) {
                                mRefreshLayout.setLoadmoreFinished(true);
                            } else {
                                mPage++;
                                mRefreshLayout.setLoadmoreFinished(false);
                            }
                            mAgentAdapter.addData(tmp);
                        }

                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        stopRefreshLoad();
                    }

                    @Override
                    protected void onFinish() {
                        dismissLoadingDialog();
                    }

                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                        if (mAgentItemList == null) {
                            showLoadingDialog();
                        }
                    }
                });
    }

}
